﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace work7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private Graphics graphics;

        private int n;
        private double length;


        double getth1;
        double getth2;

        //角度
        //1为左
        private double th1 = 30 * Math.PI / 180;
        private double th2 = 20 * Math.PI / 180;

        //长度比
        private double per1 = 0.6;
        private double per2 = 0.7;

        //笔颜色
        Pen pen = new Pen(Color.Red);

        void drawCayleyTree(int n ,double x0,double y0,double leng,double th)
        {
            if(n == 0)
                return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);

        }

        void drawLine(double x0 ,double y0 , double x1 , double y1)
        {
            graphics.DrawLine(pen, (int)x0, (int)y0 ,(int)x1 , (int)y1);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (graphics == null)
            {
                graphics = this.panel1.CreateGraphics();
            }
            else
            {
                graphics.Clear(Color.Navy);
                graphics.Dispose();
                graphics = this.panel1.CreateGraphics();
            }

            length = int.Parse(textBox1.Text);

            n = (int)numericUpDown4.Value;

            getth1 = (double)numericUpDown1.Value;
            getth2 = (double)numericUpDown3.Value;
            th1 = getth1 * Math.PI / 180;
            th2 = getth2 * Math.PI / 180;

            per1 = ((double)numericUpDown5.Value/100);
            per2 = ((double)numericUpDown2.Value/100);

            if (radioButton1.Checked) pen.Color = Color.Red;
            if (radioButton2.Checked) pen.Color = Color.Green;
            if (radioButton3.Checked) pen.Color = Color.Blue;

            drawCayleyTree(n,200,310, length, -Math.PI/2);

        }

    }
}
